package com.at.controller;

import com.at.domain.Tag;
import com.at.service.BlogService;
import com.at.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * @author : code1997
 * @date :2020-10-2020/10/17 11:46
 */
@Controller
public class TagShowController {

    @Autowired
    private TagService tagService;
    @Autowired
    private BlogService blogService;

    @GetMapping({"tags/{id}"})
    public String helloController2(@PageableDefault(size = 8,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                                   @PathVariable Long id, Model model){
        //相当于查询全表
        List<Tag> tags = tagService.listTagTop(10000);
        if (id==-1){
            Tag tag = tags.get(0);
            /*避免没有类型导致出现空指针异常的现象*/
            if (tag!=null){
                id=tag.getId();
            }
        }
        model.addAttribute("tags", tags);
        model.addAttribute("page", blogService.listBlogByTagId(pageable, id));
        model.addAttribute("activeTagId", id);
        return "tags";
    }
}
